<?php
class Tourism_order_product_model extends CI_Model
{
    private $table = 'tourism_order_product';

    public function getProductByOrderId($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->get($this->table);
    }
    
    public function getProductByProductId($params)
    {
        if (!empty($params['order_id'])) {
            $this->db->where('order_id', $params['order_id']);
        }
        if (!empty($params['order_product_id'])) {
            $this->db->where('order_product_id', $params['order_product_id']);
        }
        if (!empty($params['goods_attr_id'])) {
            $this->db->where('goods_attr_id', $params['goods_attr_id']);
        }
        return $this->db->get($this->table);
    }
    
    /*
     * 获取全部商品数量
    * */
    public function getAllProduct($order_id)
    {
        $this->db->select_sum('p.number');
        $this->db->where('p.order_id', $order_id);
        $result = $this->db->get($this->table . ' as p');
        $row = $result->row();
        return isset($row->number) ? $row->number : 0;
    }
    
    /**
     * 退货审核的时候 拒绝的时候返回库存
     * @param unknown $params
     */
    public function setTourismNum($params)
    {
        if (isset($params['refund_num'])) {
            $this->db->set('refund_num', 'refund_num+'.$params['refund_num'], false);
        }
        if (isset($params['barter_num'])) {
            $this->db->set('barter_num', 'barter_num-'.$params['barter_num'], false);
        }
        $this->db->where('order_id', $params['order_id']);
        $this->db->where('goods_attr_id', $params['goods_attr_id']);
        return $this->db->update($this->table);
    }
    
}